package {
	import flash.display.*; 
	import flash.events.*; 
 	import flash.net.*;
	import net.slideshowpro.*;
	public class Main extends MovieClip {
		private var my_ssp:SlideShowPro;
		private var my_preLoader:MovieClip;
		// The variable to which the loaded XML will be assigned 
		private var ssp_parm:XML; 
		private var sspWidth;
		private var sspHeight;
		// The object used to load the XML 
		private var urlLoader:URLLoader;
		var baseUrl:String;
		public function Main()
		{
			super();
			stage.scaleMode = StageScaleMode.NO_SCALE; 
			//stage.addEventListener(Event.RESIZE, resizeListener);
			//trace("foo");
			getParamXML();
			//initsSlideShow();
		}
		function onSlideShowData(event:SSPDataEvent) {  
			if (event.type=="galleryData") {  
				//trace("xml was loaded`");
				removeChild(my_preLoader);
		  }
			if (event.type=="xmlData") {  
				//trace(event.data);
			}
		}  
		private function getParamXML() {
			// Specify the location of the external XML
			var paramPath:String;
			
			baseUrl = root.loaderInfo.parameters.base_url;
			//trace(baseUrl);
			paramPath = root.loaderInfo.parameters.param_path;
			paramPath = (paramPath == null) ? "http://localhost/fp/sites/all/modules/ssp/swf/param.xml" : "http://"+baseUrl+paramPath;
			//trace(viewPath);
		  var urlRequest:URLRequest = new URLRequest(paramPath); 
      // Create an object that can load external text data 
      urlLoader = new URLLoader(); 
      // Register to be notified when the XML finishes loading 
      urlLoader.addEventListener(Event.COMPLETE, completeListener); 
      // Load the XML 
      urlLoader.load(urlRequest);
		}
	 // Method invoked automatically when the XML finishes loading 
    private function completeListener(e:Event):void { 
      ssp_parm = new XML(urlLoader.data); 
      //trace(ssp_parm.toXMLString());
			//trace(ssp_parm.customParams.@sspWidth.toString());
  		initsSlideShow();
	  } 
		private function initsSlideShow() {
			my_ssp = new SlideShowPro;
			my_ssp.addEventListener(SSPDataEvent.GALLERY_DATA, onSlideShowData);
			my_ssp.addEventListener(SSPDataEvent.XML_DATA, onSlideShowData);
			sspWidth = root.loaderInfo.parameters.width;
			sspWidth = (sspWidth == null) ? stage.stageWidth : sspWidth;
			sspHeight = root.loaderInfo.parameters.height;
			sspHeight = (sspHeight == null) ? stage.stageHeight : sspHeight;
			//sspWidth = 400;
			//sspHeight = 400;
			//stage.stageWidth = sspWidth;
			//stage.stageHeight = sspHeight;
			my_ssp.setSize(sspWidth,sspHeight);
			my_ssp.width = sspWidth;
			my_ssp.height = sspHeight;
			//
			my_preLoader = new preLoader;
			my_preLoader.x = sspWidth/2;
			my_preLoader.y = sspHeight/2;
			//
			for each (var child:XML in ssp_parm..*) { 
			  // If the node is an element... 
			  if (child.nodeKind() == "element") { 
			    for each (var attribute:XML in child.@*) {
			      //trace(attribute.name()+": "+attribute.toString());
			 			if(attribute.name().toString() != "sspWidth" && attribute.name().toString() != "sspHeight") {
							if (attribute.toString().search(",") == -1) {
								my_ssp[attribute.name().toString()] = attribute.toString();
							} else {
								my_ssp[attribute.name().toString()] = attribute.toString().split(",");
								//trace(attribute.toString().split(",").toString());
							}
						}
			    } 
			  } 
			} 
			//
			var viewPath:String;
			viewPath = root.loaderInfo.parameters.view_path;
			//viewPath = (viewPath == null) ? "http://mrticket.dev.linnovate.net/promoted" : viewPath;
			viewPath = (viewPath == null) ? "http://localhost/fp/ssp_opml/http:||localhost|fp|ssp_delete_this" : viewPath;
			trace("viewPath.substr(0,4): "+viewPath.substr(0,4));
			if (viewPath.substr(0,4) == 'xml/') {
				viewPath = viewPath.slice(4);
				trace(viewPath);
				trace("Default");
				viewPath = "http://"+viewPath;
				my_ssp.xmlFileType = "Default";
			} else {
					viewPath = "http://"+baseUrl+viewPath;
					my_ssp.xmlFileType = "OPML";
					trace("OPML");
			}
			my_ssp.xmlFilePath = viewPath;
			//trace(my_ssp.xmlFilePath);
			addChild(my_ssp);
			addChild(my_preLoader);
		}
					//trace(viewPath);
	}

}

